App.controller('roleController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {

    // 菜单树
    Page.ajaxPost("menu/tree.do", null, function (response) {
        $scope.allMenuTree = $.fn.zTree.init($("#allMenuTree"), {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "menuId",
                    pIdKey: "parentMenuId"
                },
                key: {
                    name: "menuName"
                }
            },
            callback: {
                onCheck : function (event, treeId, treeNode) {
                    $scope.$apply(function() {
                        var checkedNodes = $scope.allMenuTree.getCheckedNodes(true), nodes;
                        if (treeNode.checked) {// 选中，增加
                            nodes = Ztree.difference(checkedNodes, $scope.role.hasMenus, 'menuId');

                            Page.ajaxPut('roleMenu/' + $scope.role.roleId+'.do', {menuIds : Ztree.ids(nodes, 'menuId').toString()}, function() {
                                $scope.role.hasMenus = angular.copy(checkedNodes);
                            });
                        } else {// 减少
                            nodes = Ztree.difference($scope.role.hasMenus, checkedNodes, 'menuId');

                            Page.ajaxDelete('roleMenu/' + $scope.role.roleId + '.do',{menuIds : Ztree.ids(nodes, 'menuId').toString()}, function() {
                                $scope.role.hasMenus = angular.copy(checkedNodes);
                            });
                        }
                    });
                }
            }
        }, response.data);
    });
    // 部门树点击事件
    $scope.selectDept = function(dept) {
        $scope.dept = dept;
        $scope.role = null;
        $scope.roleGrid.query({deptId: dept.deptId});
    }

    // 角色表格
    $scope.roleGrid = {
        column_defs: [
            {
                target: 0,
                template: function(row){
                    return '<a href="javascript:void(0)" _index="'+row._index+'" ed-click="open">'+row.roleName+'</a>'
                }
            }
        ],
        update : function(row) {
            if (!$scope.dept) {
                $scope.dept = {};
            }
            $scope.dept.deptName = row.deptName;
            $scope.roleGrid.edit = true;
            $scope.role = row;
        },
        save: function() {
            if($scope.role._add) {
                $scope.role.deptId = $scope.dept.deptId;
            }
            Page.ajaxPost($scope.role._add ? 'role/add.do' : 'role/update.do', $scope.role, function() {
                $scope.roleGrid.cancel();
                $scope.roleGrid.query({deptId: $scope.dept ? $scope.dept.deptId : ''});
                $scope.role = null;
            });
        },
        cancel : function () {
            $scope.roleGrid.edit = false;
            $scope.role = null;
            $scope.roleGrid.query({deptId: $scope.dept.deptId});
            $scope.dept = null;
        },
        delete : function(row){
	        Page.confirm("确定要删除吗？", function () {
	            Page.ajaxPost('role/delete.do', row, function () {
	            	$scope.roleGrid.query({deptId: $scope.dept ? $scope.dept.deptId : ''});
	            });
	        });
	    },
        open : function(row) {
            $scope.roleInfo.show(event);
            $scope.role = row;
            $scope.menuEdit = false;

            // 获取角色权限
            Page.ajaxGet('roleMenu/' + $scope.role.roleId + '.do', null, function(resp) {
                var hasMenus = [];
                $(resp.data).each(function(index, menu){
                    var node = $scope.allMenuTree.getNodeByParam('menuId', menu.menuId, null);
                    if (node) {
                        menu.parentMenuId = node.parentMenuId;
                        menu.menuName = node.menuName;
                        menu.open = true;
                        hasMenus.push(menu);
                    }
                });

                $scope.role.hasMenus = hasMenus;// 角色拥有的菜单
                Ztree.unCheckAllNodes($scope.allMenuTree);
                Ztree.checkNodes($scope.allMenuTree, $scope.role.hasMenus, 'menuId');
            });
            event.stopPropagation();    //  阻止事件冒泡
        }
    };

    $scope.cancelMenu = function() {
        $scope.menuEdit = false;
        //loadMenusTree();
    };

    $scope.editMenu = function() {
        $scope.menuEdit = true;
    };

    $scope.reloadDataScope = function() {
        Page.ajaxPost('role/reloadDataScope.do', null, function() {
            Page.notice("刷新成功！");
        });
    }

    // 角色权限改变了
    $scope.$watch('role.hasMenus', function(newValue, oldValue) {
        newValue = newValue || [];
        $(newValue).each(function(index, menu){
            menu.children = null;
        });
        $.fn.zTree.init($("#hasMenuTree"), {
            data: {
                simpleData: {
                    enable: true,
                    idKey: "menuId",
                    pIdKey: "parentMenuId"
                },
                key: {
                    name: "menuName"
                }
            },
            callback: {
            }
        }, newValue);
    });
}]);
